<?php
namespace App\Http\Controllers\Home;


use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use think\Response;
use yii\debug\panels\EventPanel;
use App\Home\Manager;
use Illuminate\Support\Facades\Hash;
class ApiController extends Controller{
    //热门文章
    public function hotartical(){
        $data = DB::table('topic')
            ->select('id','title','TClickCount')
            ->orderBy('TClickCount','desc')
            ->limit(10)
            ->get();
        if($data){
            return response() -> json(['data'=>$data,'status'=>'200']);
        }else{
            return response() -> json(['没有满足要求的数据','status'=>'4001']);
        }

    }

    //最新发布
    public function newarrival(){
        $data = DB::table('topic')
            ->orderBy('update_time','desc')
            ->limit(10)
            ->get();
        if($data){
            return response() -> json(['data'=>$data,'status'=>'200']);
        }else{
            return response() -> json(['没有满足要求的数据','status'=>'4001']);
        }
    }

    //获取留言
    public function message(Request $request){
        $id = $request->input('id');
        $data = DB::table('message')
            // ->join('manager','manager.id','=','message.user_id')
            ->where('top_id',$id)
            // ->select('manager.name','message.content','manager.user_img')
            ->orderBy('created_at','desc')
            ->limit(10)
            ->get();
        if($data){
            return response() -> json(['data'=>$data,'status'=>'200']);
        }else{
            return response() -> json(['没有满足要求的数据','status'=>'4001']);
        }
    }

 //获取分类对应的文章信息
 public function artical(Request $request){
    $cate_name = $request->input('cate');
    $cateId = DB::table('cate')
                ->where('cate_name',$cate_name)
                ->value('id');
    $data = DB::table('topic')
        ->where('cate_id',$cateId)
        ->get();
    if(count($data)){
        return response() -> json(['data'=>$data,'status'=>'200']);
    }else{
        return response() -> json(['没有满足要求的数据','status'=>'4001']);
    }
}

    //获取相册分类的信息
    public function photocate(){
        $data = DB::table('photoalbum')
            ->get();

//        foreach($data as $k=>$v){
//            $data[$k]['num'] = DB::table('albumlist')
//                            ->where('photo_id',$v->id)
//                            ->count('id');
//        }
//        $data1 = json_decode($data);
        foreach($data as $k=>$v){
            $arr[$k] = DB::table('albumlist')
                            ->where('photo_id',$v->id)
                            ->count('id');
        }

        if(count($data)||count($arr)){
            return response() -> json(['data'=>$data,'data1'=>$arr,'status'=>'200']);
        }else{
            return response() -> json(['没有满足要求的数据','status'=>'4001']);
        }
    }

    //获取各类图库中的全部图片
    public function allphoto(request $request){
        $cate_name = $request->input('cate_name');
        $data = DB::table('albumlist')
            ->join('photoalbum','photoalbum.id','=','albumlist.photo_id')
            ->where('photoalbum.status',1)
            ->where('cate_name',$cate_name)
            ->get();
        if($data){
            return response() -> json(['data'=>$data,'status'=>'200']);
        }else{
            return response() -> json(['没有满足要求的数据','status'=>'4001']);
        }
    }

    //留言
    public function leavemessage(){
        $top_id = request('top_id');
        $user_id = request('user_id');
        $content = request('content');
        $user_name = request('user_name');
        $user_img = request('user_img');
        $data = DB::table('message')->insert(['top_id'=>$top_id,'user_id'=>$user_id,'content'=>$content,'user_name' => $user_name,'user_img' => $user_img]);
        if($data){
            return response() -> json(['留言成功','status'=>'200']);
        }else{
            return response() -> json(['留言失败','status'=>'4001']);
        }
    }

    //博文详情
    public function detail(){
        $id = request('id');
        $data = DB::table('topic')
            ->where('id',$id)
            ->limit(1)
            ->get();
        if($data){
            return response() -> json(['data'=>$data,'status'=>'200']);
        }else{
            return response() -> json(['没有满足要求的数据','status'=>'4001']);
        }
    }

    //登录
    public function login(Request $request){
        //接收用户输入的name password
        $name = $request->input('name');
        $password = md5($request->input('password'));
        // $data = $request->all()
        // dd($name,$password);
         
        // exit();
        //查询数据库是否有此数据
        // $data =  DB::select("select id,name,status,user_img,email from manager where name = '" + $name + "' and password = '"+ $password +"' and sta = '0'");
        $data = DB::table('manager')
            ->where('name', $name)
            ->where('password', $password)  
            ->get();
        if($data->first()){
        //   return response() -> json(['登录成功','status'=>'200']);
            return response()->json(['data'=>$data,'status'=>'200']);
        }else{
           return response() -> json(['用户名或者密码错误','status'=>'401']);
        }
    }

    //注册
    public function register(Request $request){
        //接收用户数据
        $name = $request->input('name');
        $password = $request->input('pass');
        $repwd = $request->input('repwd');
        $mobile = $request->input('mobile');
        $email = $request->input('email');
        // dd($name,$password,$repwd,$mobile,$email);
            $query1 = DB::table('manager')
                    ->where('name',$name)
                    ->get();
            // dd($query1);
            if($query1->first()){
                return response()->json(['用户名已存在', 'status' => '4001']);
            }else{
                $query2 = DB::table('manager')
                // bcrypt(
                    ->insertGetId(['name'=>$name,'password'=>MD5($password),'email'=>$email,'mobile'=>$mobile]);
                if(isset($query2)){
                    return response()->json(['msg'=>'注册成功','status'=>'200']);
                }else{
                    return response()->json(['注册失败','status'=>'4001']);
                }
            }
    }
    // 获取全部留言
    public function messageAll(Request $request){
        $id = $request->input('id');
        $data = DB::table('message')
            ->where('top_id',$id)
            ->select('*')
            ->orderBy('created_at','desc')
            ->get();
        if($data){
            return response() -> json(['data'=>$data,'status'=>'200']);
        }else{
            return response() -> json(['没有满足要求的数据','status'=>'4001']);
        }
    }
}